package cn.jsu.oj.dispatchServer.satellite.handler;

import cn.jsu.oj.dispatchServer.satellite.annotation.HandlerType;
import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.PackNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.RequestNet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;

/**
 * 委托请求处理器
 *
 * @author shan
 * @date 2022/07/03
 */
@Slf4j
@Component("requestHandler")
@HandlerType(typeNo = 3)
public class RequestHandler implements Handler {

    @Autowired
    Factory handlerFactory;

    @Override
    public void handle(PackNet<?> packNet) throws FileNotFoundException {

        RequestNet requestNet = (RequestNet) packNet.getMessage();
        log.debug("收到控制中心请求：" + requestNet.toString());
        Short command = requestNet.getCommand();
        ((Handler) handlerFactory.getProduct(command))
                .handle(packNet);
    }
}
